/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package overfeatfeatureextraction;

import avroserialization.AvroSerialization;
import java.io.ByteArrayOutputStream;
import nonmr.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

/**
 *
 * @author mwh
 */
public class HBaseResultPutter {
    
    public static void main(String args[]) throws Exception {
        HBasePutter hbasePutter = new HBasePutter();
        hbasePutter.put(null);
    }
    HTable table;
    AvroSerialization serializer;
    
    public HBaseResultPutter() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        table = new HTable(conf, "imagefeatures");
        serializer = AvroSerialization.INSTANCE;
    }
    
    public void put(String id, ImageResult imgResult) throws IOException, DecoderException {
        
        
        byte[] key = Hex.decodeHex(id.toCharArray());
        
        Put put = new Put(key);
        
        List<AvroImageFeatures> layerFeatures = imgResult.layerFeatures;
        for (AvroImageFeatures layer : layerFeatures) {
            String layerName = layer.getDescription().toString();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            serializer.serializeWithSchema(baos, layer);
            baos.close();
            byte[] byteRep = baos.toByteArray();
            put.add(Bytes.toBytes("f"), Bytes.toBytes(layerName), byteRep);
        }
        table.put(put);
    }
}
